/**
 * 缓存管理器
 * 提供多层缓存机制，优化应用性能和数据访问
 */

import { logInfo, logWarn, logError } from './errorHandler.js'

/**
 * 缓存类型枚举
 */
export const CACHE_TYPES = {
	MEMORY: 'memory',
	STORAGE: 'storage',
	SESSION: 'session',
	TEMP: 'temp'
}

/**
 * 缓存策略枚举
 */
export const CACHE_STRATEGIES = {
	LRU: 'lru', // 最近最少使用
	LFU: 'lfu', // 最少使用频率
	FIFO: 'fifo', // 先进先出
	TTL: 'ttl' // 基于时间
}

/**
 * 缓存优先级枚举
 */
export const CACHE_PRIORITIES = {
	LOW: 1,
	NORMAL: 2,
	HIGH: 3,
	CRITICAL: 4
}

/**
 * 缓存项类
 */
class CacheItem {
	constructor(key, value, options = {}) {
		this.key = key
		this.value = value
		this.createdAt = Date.now()
		this.lastAccessed = Date.now()
		this.accessCount = 0
		this.ttl = options.ttl || 0 // 0表示永不过期
		this.priority = options.priority || CACHE_PRIORITIES.NORMAL
		this.tags = options.tags || []
		this.size = this.calculateSize(value)
		this.compressed = false
		this.encrypted = false
	}
	
	/**
	 * 计算数据大小
	 * @param {any} value - 数据值
	 * @returns {number} 大小（字节）
	 */
	calculateSize(value) {
		try {
			return JSON.stringify(value).length * 2 // 粗略估算
		} catch {
			return 0
		}
	}
	
	/**
	 * 检查是否过期
	 * @returns {boolean} 是否过期
	 */
	isExpired() {
		if (this.ttl === 0) return false
		return Date.now() - this.createdAt > this.ttl
	}
	
	/**
	 * 访问缓存项
	 * @returns {any} 缓存值
	 */
	access() {
		this.lastAccessed = Date.now()
		this.accessCount++
		return this.value
	}
	
	/**
	 * 更新缓存项
	 * @param {any} newValue - 新值
	 * @param {Object} options - 选项
	 */
	update(newValue, options = {}) {
		this.value = newValue
		this.size = this.calculateSize(newValue)
		this.lastAccessed = Date.now()
		
		if (options.ttl !== undefined) {
			this.ttl = options.ttl
			this.createdAt = Date.now() // 重置创建时间
		}
		
		if (options.priority !== undefined) {
			this.priority = options.priority
		}
		
		if (options.tags !== undefined) {
			this.tags = options.tags
		}
	}
	
	/**
	 * 获取缓存项信息
	 * @returns {Object} 缓存项信息
	 */
	getInfo() {
		return {
			key: this.key,
			size: this.size,
			createdAt: this.createdAt,
			lastAccessed: this.lastAccessed,
			accessCount: this.accessCount,
			ttl: this.ttl,
			priority: this.priority,
			tags: this.tags,
			isExpired: this.isExpired(),
			compressed: this.compressed,
			encrypted: this.encrypted
		}
	}
}

/**
 * 缓存管理器类
 */
class CacheManager {
	constructor() {
		// 内存缓存
		this.memoryCache = new Map()
		
		// 缓存配置
		this.config = {
			maxMemorySize: 50 * 1024 * 1024, // 50MB
			maxMemoryItems: 1000,
			defaultTTL: 30 * 60 * 1000, // 30分钟
			cleanupInterval: 5 * 60 * 1000, // 5分钟
			strategy: CACHE_STRATEGIES.LRU,
			enableCompression: true,
			enableEncryption: false,
			compressionThreshold: 1024, // 1KB
			encryptionKey: null,
			persistentCacheSize: 100 * 1024 * 1024, // 100MB
			enableStats: true
		}
		
		// 统计信息
		this.stats = {
			hits: 0,
			misses: 0,
			sets: 0,
			deletes: 0,
			evictions: 0,
			cleanups: 0,
			totalSize: 0,
			startTime: Date.now()
		}
		
		// 清理定时器
		this.cleanupTimer = null
		
		this.init()
	}
	
	/**
	 * 初始化缓存管理器
	 */
	init() {
		this.loadConfig()
		this.startCleanupTimer()
		this.loadPersistentCache()
		
		logInfo('缓存管理器已初始化', {
			maxMemorySize: this.config.maxMemorySize,
			maxMemoryItems: this.config.maxMemoryItems,
			strategy: this.config.strategy
		})
	}
	
	/**
	 * 加载配置
	 */
	loadConfig() {
		try {
			if (typeof uni !== 'undefined') {
				const saved = uni.getStorageSync('cache_config')
				if (saved) {
					this.config = { ...this.config, ...saved }
				}
			}
		} catch (error) {
			logError('加载缓存配置失败', { error: error.message })
		}
	}
	
	/**
	 * 保存配置
	 */
	saveConfig() {
		try {
			if (typeof uni !== 'undefined') {
				uni.setStorageSync('cache_config', this.config)
			}
		} catch (error) {
			logError('保存缓存配置失败', { error: error.message })
		}
	}
	
	/**
	 * 启动清理定时器
	 */
	startCleanupTimer() {
		if (this.cleanupTimer) {
			clearInterval(this.cleanupTimer)
		}
		
		this.cleanupTimer = setInterval(() => {
			this.cleanup()
		}, this.config.cleanupInterval)
	}
	
	/**
	 * 加载持久化缓存
	 */
	loadPersistentCache() {
		try {
			if (typeof uni !== 'undefined') {
				const keys = uni.getStorageInfoSync().keys
				const cacheKeys = keys.filter(key => key.startsWith('cache_persistent_'))
				
				cacheKeys.forEach(storageKey => {
					try {
						const data = uni.getStorageSync(storageKey)
						if (data && data.item) {
							const item = new CacheItem(data.item.key, data.item.value, {
								ttl: data.item.ttl,
								priority: data.item.priority,
								tags: data.item.tags
							})
							
							// 恢复时间信息
							item.createdAt = data.item.createdAt
							item.lastAccessed = data.item.lastAccessed
							item.accessCount = data.item.accessCount
							
							// 检查是否过期
							if (!item.isExpired()) {
								this.memoryCache.set(item.key, item)
							} else {
								// 删除过期的持久化缓存
								uni.removeStorageSync(storageKey)
							}
						}
					} catch (error) {
						logWarn('加载持久化缓存项失败', { key: storageKey, error: error.message })
						uni.removeStorageSync(storageKey)
					}
				})
			}
		} catch (error) {
			logError('加载持久化缓存失败', { error: error.message })
		}
	}
	
	/**
	 * 设置缓存
	 * @param {string} key - 缓存键
	 * @param {any} value - 缓存值
	 * @param {Object} options - 选项
	 * @returns {boolean} 是否设置成功
	 */
	set(key, value, options = {}) {
		try {
			const cacheOptions = {
				ttl: options.ttl || this.config.defaultTTL,
				priority: options.priority || CACHE_PRIORITIES.NORMAL,
				tags: options.tags || [],
				type: options.type || CACHE_TYPES.MEMORY,
				persistent: options.persistent || false
			}
			
			// 创建缓存项
			const item = new CacheItem(key, value, cacheOptions)
			
			// 压缩处理
			if (this.config.enableCompression && item.size > this.config.compressionThreshold) {
				item.value = this.compress(item.value)
				item.compressed = true
			}
			
			// 加密处理
			if (this.config.enableEncryption && this.config.encryptionKey) {
				item.value = this.encrypt(item.value)
				item.encrypted = true
			}
			
			// 检查内存限制
			if (!this.checkMemoryLimits(item)) {
				this.evictItems(item.size)
			}
			
			// 设置到内存缓存
			this.memoryCache.set(key, item)
			
			// 持久化缓存
			if (cacheOptions.persistent) {
				this.setPersistent(key, item)
			}
			
			// 更新统计
			if (this.config.enableStats) {
				this.stats.sets++
				this.updateTotalSize()
			}
			
			return true
		} catch (error) {
			logError('设置缓存失败', { key, error: error.message })
			return false
		}
	}
	
	/**
	 * 获取缓存
	 * @param {string} key - 缓存键
	 * @param {Object} options - 选项
	 * @returns {any} 缓存值
	 */
	get(key, options = {}) {
		try {
			const item = this.memoryCache.get(key)
			
			if (!item) {
				// 尝试从持久化缓存加载
				const persistentItem = this.getPersistent(key)
				if (persistentItem) {
					this.memoryCache.set(key, persistentItem)
					return this.processGetResult(persistentItem, options)
				}
				
				// 缓存未命中
				if (this.config.enableStats) {
					this.stats.misses++
				}
				return undefined
			}
			
			// 检查是否过期
			if (item.isExpired()) {
				this.delete(key)
				if (this.config.enableStats) {
					this.stats.misses++
				}
				return undefined
			}
			
			// 缓存命中
			if (this.config.enableStats) {
				this.stats.hits++
			}
			
			return this.processGetResult(item, options)
		} catch (error) {
			logError('获取缓存失败', { key, error: error.message })
			return undefined
		}
	}
	
	/**
	 * 处理获取结果
	 * @param {CacheItem} item - 缓存项
	 * @param {Object} options - 选项
	 * @returns {any} 处理后的值
	 */
	processGetResult(item, options) {
		let value = item.access()
		
		// 解密处理
		if (item.encrypted) {
			value = this.decrypt(value)
		}
		
		// 解压缩处理
		if (item.compressed) {
			value = this.decompress(value)
		}
		
		// 返回副本（如果需要）
		if (options.clone !== false) {
			return this.deepClone(value)
		}
		
		return value
	}
	
	/**
	 * 删除缓存
	 * @param {string} key - 缓存键
	 * @returns {boolean} 是否删除成功
	 */
	delete(key) {
		try {
			const deleted = this.memoryCache.delete(key)
			
			// 删除持久化缓存
			this.deletePersistent(key)
			
			if (deleted && this.config.enableStats) {
				this.stats.deletes++
				this.updateTotalSize()
			}
			
			return deleted
		} catch (error) {
			logError('删除缓存失败', { key, error: error.message })
			return false
		}
	}
	
	/**
	 * 检查缓存是否存在
	 * @param {string} key - 缓存键
	 * @returns {boolean} 是否存在
	 */
	has(key) {
		const item = this.memoryCache.get(key)
		return item && !item.isExpired()
	}
	
	/**
	 * 清空所有缓存
	 */
	clear() {
		try {
			this.memoryCache.clear()
			this.clearPersistent()
			
			if (this.config.enableStats) {
				this.stats.totalSize = 0
			}
			
			logInfo('缓存已清空')
		} catch (error) {
			logError('清空缓存失败', { error: error.message })
		}
	}
	
	/**
	 * 根据标签删除缓存
	 * @param {string|Array} tags - 标签
	 * @returns {number} 删除的数量
	 */
	deleteByTags(tags) {
		const targetTags = Array.isArray(tags) ? tags : [tags]
		let deletedCount = 0
		
		for (const [key, item] of this.memoryCache) {
			if (item.tags.some(tag => targetTags.includes(tag))) {
				this.delete(key)
				deletedCount++
			}
		}
		
		return deletedCount
	}
	
	/**
	 * 根据模式删除缓存
	 * @param {RegExp|string} pattern - 模式
	 * @returns {number} 删除的数量
	 */
	deleteByPattern(pattern) {
		const regex = pattern instanceof RegExp ? pattern : new RegExp(pattern)
		let deletedCount = 0
		
		for (const key of this.memoryCache.keys()) {
			if (regex.test(key)) {
				this.delete(key)
				deletedCount++
			}
		}
		
		return deletedCount
	}
	
	/**
	 * 获取或设置缓存（如果不存在则设置）
	 * @param {string} key - 缓存键
	 * @param {Function|any} valueOrFactory - 值或工厂函数
	 * @param {Object} options - 选项
	 * @returns {any} 缓存值
	 */
	getOrSet(key, valueOrFactory, options = {}) {
		let value = this.get(key, options)
		
		if (value === undefined) {
			if (typeof valueOrFactory === 'function') {
				value = valueOrFactory()
			} else {
				value = valueOrFactory
			}
			
			this.set(key, value, options)
		}
		
		return value
	}
	
	/**
	 * 批量设置缓存
	 * @param {Object} items - 缓存项对象
	 * @param {Object} options - 选项
	 * @returns {number} 成功设置的数量
	 */
	batchSet(items, options = {}) {
		let successCount = 0
		
		Object.entries(items).forEach(([key, value]) => {
			if (this.set(key, value, options)) {
				successCount++
			}
		})
		
		return successCount
	}
	
	/**
	 * 批量获取缓存
	 * @param {Array} keys - 缓存键数组
	 * @param {Object} options - 选项
	 * @returns {Object} 缓存值对象
	 */
	batchGet(keys, options = {}) {
		const result = {}
		
		keys.forEach(key => {
			const value = this.get(key, options)
			if (value !== undefined) {
				result[key] = value
			}
		})
		
		return result
	}
	
	/**
	 * 批量删除缓存
	 * @param {Array} keys - 缓存键数组
	 * @returns {number} 删除的数量
	 */
	batchDelete(keys) {
		let deletedCount = 0
		
		keys.forEach(key => {
			if (this.delete(key)) {
				deletedCount++
			}
		})
		
		return deletedCount
	}
	
	/**
	 * 检查内存限制
	 * @param {CacheItem} newItem - 新缓存项
	 * @returns {boolean} 是否在限制内
	 */
	checkMemoryLimits(newItem) {
		const currentSize = this.getCurrentSize()
		const currentCount = this.memoryCache.size
		
		return (currentSize + newItem.size <= this.config.maxMemorySize) &&
			   (currentCount < this.config.maxMemoryItems)
	}
	
	/**
	 * 驱逐缓存项
	 * @param {number} requiredSize - 需要的空间大小
	 */
	evictItems(requiredSize) {
		const strategy = this.config.strategy
		let freedSize = 0
		let evictedCount = 0
		
		// 获取排序后的缓存项
		const sortedItems = this.getSortedItemsForEviction(strategy)
		
		// 驱逐缓存项直到满足空间要求
		for (const [key, item] of sortedItems) {
			if (freedSize >= requiredSize && this.memoryCache.size <= this.config.maxMemoryItems) {
				break
			}
			
			// 保护高优先级缓存
			if (item.priority >= CACHE_PRIORITIES.HIGH) {
				continue
			}
			
			freedSize += item.size
			this.memoryCache.delete(key)
			evictedCount++
		}
		
		if (this.config.enableStats) {
			this.stats.evictions += evictedCount
			this.updateTotalSize()
		}
		
		logInfo('缓存驱逐完成', {
			strategy,
			evictedCount,
			freedSize,
			remainingItems: this.memoryCache.size
		})
	}
	
	/**
	 * 获取用于驱逐的排序缓存项
	 * @param {string} strategy - 驱逐策略
	 * @returns {Array} 排序后的缓存项
	 */
	getSortedItemsForEviction(strategy) {
		const items = Array.from(this.memoryCache.entries())
		
		switch (strategy) {
			case CACHE_STRATEGIES.LRU:
				return items.sort((a, b) => a[1].lastAccessed - b[1].lastAccessed)
			
			case CACHE_STRATEGIES.LFU:
				return items.sort((a, b) => a[1].accessCount - b[1].accessCount)
			
			case CACHE_STRATEGIES.FIFO:
				return items.sort((a, b) => a[1].createdAt - b[1].createdAt)
			
			case CACHE_STRATEGIES.TTL:
				return items.sort((a, b) => {
					const aExpiry = a[1].createdAt + a[1].ttl
					const bExpiry = b[1].createdAt + b[1].ttl
					return aExpiry - bExpiry
				})
			
			default:
				return items
		}
	}
	
	/**
	 * 清理过期缓存
	 */
	cleanup() {
		let cleanedCount = 0
		
		for (const [key, item] of this.memoryCache) {
			if (item.isExpired()) {
				this.delete(key)
				cleanedCount++
			}
		}
		
		if (this.config.enableStats) {
			this.stats.cleanups++
			this.updateTotalSize()
		}
		
		if (cleanedCount > 0) {
			logInfo('缓存清理完成', { cleanedCount })
		}
	}
	
	/**
	 * 设置持久化缓存
	 * @param {string} key - 缓存键
	 * @param {CacheItem} item - 缓存项
	 */
	setPersistent(key, item) {
		try {
			if (typeof uni !== 'undefined') {
				const storageKey = `cache_persistent_${key}`
				const data = {
					item: {
						key: item.key,
						value: item.value,
						ttl: item.ttl,
						priority: item.priority,
						tags: item.tags,
						createdAt: item.createdAt,
						lastAccessed: item.lastAccessed,
						accessCount: item.accessCount
					}
				}
				
				uni.setStorageSync(storageKey, data)
			}
		} catch (error) {
			logError('设置持久化缓存失败', { key, error: error.message })
		}
	}
	
	/**
	 * 获取持久化缓存
	 * @param {string} key - 缓存键
	 * @returns {CacheItem|null} 缓存项
	 */
	getPersistent(key) {
		try {
			if (typeof uni !== 'undefined') {
				const storageKey = `cache_persistent_${key}`
				const data = uni.getStorageSync(storageKey)
				
				if (data && data.item) {
					const item = new CacheItem(data.item.key, data.item.value, {
						ttl: data.item.ttl,
						priority: data.item.priority,
						tags: data.item.tags
					})
					
					// 恢复时间信息
					item.createdAt = data.item.createdAt
					item.lastAccessed = data.item.lastAccessed
					item.accessCount = data.item.accessCount
					
					return item.isExpired() ? null : item
				}
			}
		} catch (error) {
			logError('获取持久化缓存失败', { key, error: error.message })
		}
		
		return null
	}
	
	/**
	 * 删除持久化缓存
	 * @param {string} key - 缓存键
	 */
	deletePersistent(key) {
		try {
			if (typeof uni !== 'undefined') {
				const storageKey = `cache_persistent_${key}`
				uni.removeStorageSync(storageKey)
			}
		} catch (error) {
			logError('删除持久化缓存失败', { key, error: error.message })
		}
	}
	
	/**
	 * 清空持久化缓存
	 */
	clearPersistent() {
		try {
			if (typeof uni !== 'undefined') {
				const keys = uni.getStorageInfoSync().keys
				const cacheKeys = keys.filter(key => key.startsWith('cache_persistent_'))
				
				cacheKeys.forEach(key => {
					uni.removeStorageSync(key)
				})
			}
		} catch (error) {
			logError('清空持久化缓存失败', { error: error.message })
		}
	}
	
	/**
	 * 压缩数据
	 * @param {any} data - 要压缩的数据
	 * @returns {string} 压缩后的数据
	 */
	compress(data) {
		// 简单的压缩实现（实际应用中可以使用更好的压缩算法）
		try {
			const jsonString = JSON.stringify(data)
			// 这里可以集成真正的压缩库
			return jsonString
		} catch (error) {
			logError('压缩数据失败', { error: error.message })
			return data
		}
	}
	
	/**
	 * 解压缩数据
	 * @param {string} compressedData - 压缩的数据
	 * @returns {any} 解压缩后的数据
	 */
	decompress(compressedData) {
		try {
			// 简单的解压缩实现
			return JSON.parse(compressedData)
		} catch (error) {
			logError('解压缩数据失败', { error: error.message })
			return compressedData
		}
	}
	
	/**
	 * 加密数据
	 * @param {any} data - 要加密的数据
	 * @returns {string} 加密后的数据
	 */
	encrypt(data) {
		// 简单的加密实现（实际应用中应使用安全的加密算法）
		try {
			const jsonString = JSON.stringify(data)
			// 这里可以集成真正的加密库
			return btoa(jsonString) // Base64编码作为示例
		} catch (error) {
			logError('加密数据失败', { error: error.message })
			return data
		}
	}
	
	/**
	 * 解密数据
	 * @param {string} encryptedData - 加密的数据
	 * @returns {any} 解密后的数据
	 */
	decrypt(encryptedData) {
		try {
			// 简单的解密实现
			const jsonString = atob(encryptedData) // Base64解码
			return JSON.parse(jsonString)
		} catch (error) {
			logError('解密数据失败', { error: error.message })
			return encryptedData
		}
	}
	
	/**
	 * 深度克隆对象
	 * @param {any} obj - 要克隆的对象
	 * @returns {any} 克隆后的对象
	 */
	deepClone(obj) {
		if (obj === null || typeof obj !== 'object') {
			return obj
		}
		
		if (obj instanceof Date) {
			return new Date(obj.getTime())
		}
		
		if (obj instanceof Array) {
			return obj.map(item => this.deepClone(item))
		}
		
		if (typeof obj === 'object') {
			const cloned = {}
			for (const key in obj) {
				if (obj.hasOwnProperty(key)) {
					cloned[key] = this.deepClone(obj[key])
				}
			}
			return cloned
		}
		
		return obj
	}
	
	/**
	 * 获取当前缓存大小
	 * @returns {number} 当前大小
	 */
	getCurrentSize() {
		let totalSize = 0
		for (const item of this.memoryCache.values()) {
			totalSize += item.size
		}
		return totalSize
	}
	
	/**
	 * 更新总大小统计
	 */
	updateTotalSize() {
		this.stats.totalSize = this.getCurrentSize()
	}
	
	/**
	 * 获取缓存统计信息
	 * @returns {Object} 统计信息
	 */
	getStats() {
		const hitRate = this.stats.hits + this.stats.misses > 0 
			? (this.stats.hits / (this.stats.hits + this.stats.misses) * 100).toFixed(2)
			: 0
		
		return {
			...this.stats,
			hitRate: `${hitRate}%`,
			currentItems: this.memoryCache.size,
			currentSize: this.getCurrentSize(),
			maxSize: this.config.maxMemorySize,
			maxItems: this.config.maxMemoryItems,
			uptime: Date.now() - this.stats.startTime
		}
	}
	
	/**
	 * 获取缓存信息
	 * @returns {Array} 缓存信息列表
	 */
	getCacheInfo() {
		const info = []
		
		for (const [key, item] of this.memoryCache) {
			info.push(item.getInfo())
		}
		
		return info.sort((a, b) => b.lastAccessed - a.lastAccessed)
	}
	
	/**
	 * 获取所有缓存键
	 * @returns {Array} 缓存键列表
	 */
	getKeys() {
		return Array.from(this.memoryCache.keys())
	}
	
	/**
	 * 获取缓存大小
	 * @returns {number} 缓存项数量
	 */
	size() {
		return this.memoryCache.size
	}
	
	/**
	 * 更新配置
	 * @param {Object} newConfig - 新配置
	 */
	updateConfig(newConfig) {
		this.config = { ...this.config, ...newConfig }
		this.saveConfig()
		
		// 重启清理定时器
		this.startCleanupTimer()
		
		logInfo('缓存配置已更新', newConfig)
	}
	
	/**
	 * 导出缓存数据
	 * @returns {Object} 导出的数据
	 */
	export() {
		const data = {
			config: this.config,
			stats: this.stats,
			items: {},
			exportTime: new Date().toISOString()
		}
		
		for (const [key, item] of this.memoryCache) {
			data.items[key] = {
				value: item.value,
				ttl: item.ttl,
				priority: item.priority,
				tags: item.tags,
				createdAt: item.createdAt,
				lastAccessed: item.lastAccessed,
				accessCount: item.accessCount
			}
		}
		
		return data
	}
	
	/**
	 * 导入缓存数据
	 * @param {Object} data - 导入的数据
	 * @returns {boolean} 是否导入成功
	 */
	import(data) {
		try {
			if (data.config) {
				this.config = { ...this.config, ...data.config }
			}
			
			if (data.items) {
				Object.entries(data.items).forEach(([key, itemData]) => {
					const item = new CacheItem(key, itemData.value, {
						ttl: itemData.ttl,
						priority: itemData.priority,
						tags: itemData.tags
					})
					
					item.createdAt = itemData.createdAt
					item.lastAccessed = itemData.lastAccessed
					item.accessCount = itemData.accessCount
					
					if (!item.isExpired()) {
						this.memoryCache.set(key, item)
					}
				})
			}
			
			this.updateTotalSize()
			return true
		} catch (error) {
			logError('导入缓存数据失败', { error: error.message })
			return false
		}
	}
	
	/**
	 * 销毁缓存管理器
	 */
	destroy() {
		if (this.cleanupTimer) {
			clearInterval(this.cleanupTimer)
			this.cleanupTimer = null
		}
		
		this.saveConfig()
		this.clear()
		
		logInfo('缓存管理器已销毁')
	}
}

// 创建全局实例
const cacheManager = new CacheManager()

// 便捷方法
export const setCache = (key, value, options) => {
	return cacheManager.set(key, value, options)
}

export const getCache = (key, options) => {
	return cacheManager.get(key, options)
}

export const deleteCache = (key) => {
	return cacheManager.delete(key)
}

export const hasCache = (key) => {
	return cacheManager.has(key)
}

export const clearCache = () => {
	cacheManager.clear()
}

export const getCacheOrSet = (key, valueOrFactory, options) => {
	return cacheManager.getOrSet(key, valueOrFactory, options)
}

export const batchSetCache = (items, options) => {
	return cacheManager.batchSet(items, options)
}

export const batchGetCache = (keys, options) => {
	return cacheManager.batchGet(keys, options)
}

export const batchDeleteCache = (keys) => {
	return cacheManager.batchDelete(keys)
}

export const deleteCacheByTags = (tags) => {
	return cacheManager.deleteByTags(tags)
}

export const deleteCacheByPattern = (pattern) => {
	return cacheManager.deleteByPattern(pattern)
}

export const getCacheStats = () => {
	return cacheManager.getStats()
}

export const getCacheInfo = () => {
	return cacheManager.getCacheInfo()
}

export const updateCacheConfig = (config) => {
	cacheManager.updateConfig(config)
}

export const exportCacheData = () => {
	return cacheManager.export()
}

export const importCacheData = (data) => {
	return cacheManager.import(data)
}

// 导出管理器实例
export default cacheManager